package com.neouton.crm.generalPayRecord.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.config.ResponseAPI;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.generalPayRecord.entity.GeneralPayRecord;
import com.neouton.crm.generalPayRecord.service.GeneralPayRecordService;
import com.neouton.crm.generalPayRequest.service.GeneralPayRequestService;
import com.neouton.crm.pm.service.ProjectMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/generalPayRecord")
public class GeneralPayRecordController {

    @Resource
    private GeneralPayRecordService generalPayRecordService;

    @Resource
    private GeneralPayRequestService generalPayRequestService;

    @Resource
    private ProjectMemberService projectMemberService;


    /**
     * 列表信息查询
     *
     */
    @PostMapping("/queryGeneralPaymentRecordsList/{pageNum}/{pageSize}")
    public ResponseAPI queryGeneralPaymentRecordsList(@PathVariable(value = "pageNum") Integer pageNum,
                                                  @PathVariable(value = "pageSize") Integer pageSize,
                                                  @RequestBody GeneralPayRecord generalPayRecord,
                                                  @RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"generalPayRecord", token);
        IPage<GeneralPayRecord> page = new Page<>(pageNum, pageSize, true);
        PageUtil<GeneralPayRecord> list = generalPayRecordService.queryGeneralPaymentRecordsList(page, generalPayRecord, userList, token);
        return ResponseAPI.successData(list);
    }




    /**
     * 列表信息查询
     *
     */
    @PostMapping("/queryGeneralPaymentRecordsApprovalList/{pageNum}/{pageSize}")
    public ResponseAPI queryManagerPaymentRecordsApprovalList(@PathVariable(value = "pageNum") Integer pageNum,
                                                      @PathVariable(value = "pageSize") Integer pageSize,
                                                      @RequestBody GeneralPayRecord managerPayRecord,
                                                      @RequestHeader("token") String token) {

        IPage<GeneralPayRecord> page = new Page<>(pageNum, pageSize, true);
        PageUtil<GeneralPayRecord> list = generalPayRecordService.queryGeneralPaymentRecordsApprovalList(page, managerPayRecord, token);
        return ResponseAPI.successData(list);
    }

    /**
     * 查询付款记录
     * @param entity
     * @return
     */
    @PostMapping("/queryGeneralPayRecordedMoney/")
    public ResponseAPI queryGeneralPayRecordedMoney(@RequestBody GeneralPayRecord entity) {
        List<GeneralPayRecord> list =  generalPayRecordService.queryGeneralPayRecordedMoney(entity);
        return ResponseAPI.successData(list);
    }


    @PostMapping("/saveGeneralPaymentRecord")
    public ResponseAPI saveManagePaymentRecord(@RequestBody GeneralPayRecord entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(generalPayRecordService.saveManagePaymentRecord(entity,token));
    }

    @PostMapping("/approvedGeneralPayRecord/")
    public ResponseAPI approvedManagerPayRecord(@RequestBody GeneralPayRecord entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(generalPayRecordService.approvedManagerPayRecord(entity,token,false));
    }

    @PostMapping("/rejectionGeneralPayRecord")
    public ResponseAPI rejectionManagerPayRequest(@RequestBody GeneralPayRecord entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(generalPayRecordService.rejectionManagerPayRecord(entity,token));
    }

    @PostMapping("/queryGeneralPayRecordHistory/{approvalId}")
    public ResponseAPI queryManagerPayRecordHistory(@PathVariable(value = "approvalId") Integer approvalId) {
        return ResponseAPI.successData(generalPayRecordService.queryManagerPayRecordHistory(approvalId));
    }

    @PostMapping("/deletePayRecordsById/{payRecordsId}")
    public ResponseAPI deletePayedRequestById(@PathVariable(value = "payRecordsId") Integer payRecordsId) {
        Boolean flag= generalPayRecordService.deletePayRecordsById(payRecordsId);
        return ResponseAPI.successData(flag);
    }


    @PostMapping("/updatePrIsNotById/{payRequestId}")
    public ResponseAPI updatePrIsNotById(@PathVariable(value = "payRequestId") Integer payRequestId) {
        generalPayRequestService.updatePrIsNotById(payRequestId);
        return ResponseAPI.success();
    }
    /**
     * Excel
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, @RequestBody GeneralPayRecord entity, @RequestHeader("token") String token) throws Exception{
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"generalPayRecord", token);
        List<GeneralPayRecord> list = generalPayRecordService.queryPaymentRecordsListInfo(entity, userList, token);
        generalPayRecordService.exportPaymentRecordsInfo(response,list,token);
    }

}
